
<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="司机名称" prop="driverName">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.driverName" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="司机电话" prop="driverPhone">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.driverPhone" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="供应商" prop="vehicleTypeIds">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.supplierId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.status" placeholder="请选择" style="width: 256px">
            <el-option :value="0" label="待处理"></el-option>
            <el-option :value="1" label="成功"></el-option>
            <el-option :value="2" label="失败"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算策略" prop="strategy">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.strategy" placeholder="请选择" style="width: 256px">
            <el-option :value="1">周结算</el-option>
            <el-option :value="2">日结算</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平台" prop="platformId">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.platformId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            style="width: 256px"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleCreateTimeChange"
            v-model="createdTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="审核时间">
          <el-date-picker
            style="width: 256px"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleAuditTimeChange"
            v-model="auditTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <div style="display: flex">
          <el-button type="warning" @click="handleExport">
            <template #icon><Edit /></template>
            导出
          </el-button>
          <el-button :disabled="ids.length === 0" @click="batchHandleDelete(1)" type="primary">
            <template #icon><Delete /></template>
            批量通过
          </el-button>
          <el-button :disabled="ids.length === 0" @click="batchHandleDelete(2)" type="danger">
            <template #icon><Delete /></template>
            批量拒绝
          </el-button>
          <div v-if="totalPrice" style="display: flex; align-items: center">
            <div>总金额</div>
            <div style="color: red">{{ totalPrice }}</div>
          </div>
        </div>
      </template>
      <el-table v-loading="loading" :data="TableData" @selection-change="handleSelectionChange" highlight-current-row row-key="id">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="姓名" min-width="120" align="center" prop="driverName"></el-table-column>
        <el-table-column label="手机" min-width="120" align="center" prop="driverPhone"></el-table-column>
        <el-table-column label="供应商" min-width="120" align="center" prop="supplierName"></el-table-column>
        <el-table-column label="提现金额" min-width="120" align="center" prop="fund"></el-table-column>
        <el-table-column label="账户余额" min-width="120" align="center" prop="accountFund">
          <template #default="scope">
            <div v-if="scope.row.accountFund <= 0" style="color: red">{{ scope.row.accountFund }}</div>
            <div v-else>{{ scope.row.accountFund }}</div>
          </template>
        </el-table-column>
        <el-table-column label="平台" min-width="120" align="center" prop="platformId">
          <template #default="scope">
            <div>{{ platFormEnum[scope.row.platformId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="提现策略" min-width="120" align="center" prop="strategy">
          <template #default="scope">
            <div v-if="scope.row.strategy == 1">周提现</div>
            <div v-else>日提现</div>
          </template>
        </el-table-column>
        <el-table-column label="类型" min-width="120" align="center" prop="type">
          <template #default="scope">
            <div v-if="scope.row.type == 1">支付宝</div>
            <div v-else>其他</div>
          </template>
        </el-table-column>
        <el-table-column label="提现账号" min-width="120" align="center" prop="account"></el-table-column>
        <el-table-column label="提现状态" min-width="120" align="center" prop="status">
          <template #default="scope">
            <div v-if="scope.row.status == 0" style="color: orange">待处理</div>
            <div v-else-if="scope.row.status == 1" style="color: #55d187">成功</div>
            <div v-else style="color: red">失败</div>
          </template>
        </el-table-column>
        <el-table-column label="审核理由" min-width="120" align="center" prop="reason"></el-table-column>
        <el-table-column label="审核人" min-width="120" align="center" prop="adminName"></el-table-column>
        <el-table-column label="创建时间" min-width="120" align="center" prop="createTime"></el-table-column>
        <el-table-column label="审核时间" min-width="120" align="center" prop="updateTime"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div style="display: flex; justify-content: center; align-items: center">
              <el-button type="primary" link size="small" @click.stop="handleAudit(scope.row)">
                <template #icon><Pointer /></template>
                审核
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        :page-sizes="[10, 50, 100, 500]"
        v-model:total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <!-- 改派 -->
    <EditModal ref="editModalRef" @success="handleQuery" />
    <el-dialog v-model="isShoeConfirmAudit" title="审核" width="600" :before-close="handleClose">
      <div style="display: flex; align-items: center; padding: 40px 0">
        <div style="margin-right: 14px">原因:</div>
        <el-input style="width: 400px" v-model="reason" placeholder="请输入"></el-input>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});

import CommonApi from "@/api/common";
import ExportApi from "@/api/exports";
import { carTypeArr, platFormArr, xcCarTypeArr, fzCarTypeArr, nhCarTypeArr, hnCarTypeArr, xcCarEnum, fzCarTypeEnum, nhCarTypeEnum, hvCarEnum, platFormEnum } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import DriverApi from "@/api/driver";
import EditModal from "./components/editModal.vue";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();
const useTimes = ref();
const createdTimes = ref();
const editModalRef = ref(null);
const isShoeConfirmAudit = ref(false);
const reason = ref(`提现信息不正确，请重新输入！
1、检查个人信息姓名、身份证号、手机号是否正确。
2、提现账号与接单手机号码的个人信息必须一致。
上述信息如有误，请联系车队进行修改，重新申请提现。`);
const createTimes = ref();
const auditTimes = ref();

const loading = ref(false);
const totalPrice = ref();

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);
const ids = ref<number[]>([]);

function handleExport() {
  ExportApi.withdrawExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "司机" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

//审核人
function handleAudit(row: any) {
  editModalRef.value.edit(row);
}
// 查询菜单
function handleQuery() {
  loading.value = true;
  DriverApi.getDriverWithRecords(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 批量删除 */
async function batchHandleDelete(type?: number) {
  if (type == 1) {
    const roleIds = ids.value.join(",");
    //发送审核请求
    ElMessageBox.confirm("确认审核?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      DriverApi.driverConfirmAudit({ id: roleIds, op: type })
        .then(() => {
          ElMessage.success("删除成功");
          handleQuery();
        })
        .finally(() => (loading.value = false));
    });
  } else {
    isShoeConfirmAudit.value = true;
  }
}
function handleClose() {
  isShoeConfirmAudit.value = false;
  reason.value = "";
}
function handleSubmit() {
  const idArr = ids.value.join(",");
  let params = {
    id: idArr,
    op: 2,
    reason: reason.value,
  };
  DriverApi.driverConfirmAudit(params).then((res) => {
    if (res.code == 0) {
      //发送审核请求
      isShoeConfirmAudit.value = false;
      ElMessage.success(res.message);
      handleQuery();
      reason.value = "";
    } else {
      ElMessage.error(res.message);
    }
  });
}

//点击创建时间搜索
function handleCreateTimeChange(times: any) {
  queryParams.createTimeFrom = `${times[0]} 00:00:00`;
  queryParams.createTimeTo = `${times[1]} 23:59:59`;
}
//点击审核时间
function handleAuditTimeChange(times: any) {
  queryParams.updateTimeFrom = `${times[0]} 00:00:00`;
  queryParams.updateTimeTo = `${times[1]} 23:59:59`;
}

// 重置查询
function handleResetQuery() {
  queryParams.createTimeFrom = undefined;
  queryParams.createTimeTo = undefined;
  queryParams.updateTimeFrom = undefined;
  queryParams.updateTimeTo = undefined;
  createTimes.value = null;
  auditTimes.value = null;
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  let sum = 0;
  selection.forEach((item: any) => {
    sum += item.fund;
  });
  if (sum > 0) {
    totalPrice.value = sum.toFixed(1);
  } else {
    totalPrice.value = "";
  }

  ids.value = selection.map((item: any) => item.id);
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
